import numpy as np
import torch
from torch.utils import data

import d2l

true_w = torch.tensor([2, -3.4])
true_b = 4.2

features, labels = d2l.synthetic_data(true_w, true_b, 1000)
batch_size = 10
data_iter = d2l.load_array((features, labels), batch_size)

from torch import nn

net = nn.Sequential(nn.Linear(2, 1))

net[0].weight.data.normal_(0, 0.01)
net[0].bias.data.fill_(0)

loss = nn.MSELoss()

trainer = torch.optim.SGD(net.parameters(), lr=0.03)

num_epochs = 3
for epoch in range(num_epochs):
    for X, y in data_iter:
        l = loss(net(X), y)
        trainer.zero_grad()
        l.backward()
        trainer.step()
    # l = loss(net(features), labels)
    # print(f'epoch {epoch + 1}, loss {l:f}')

print('true weight:')
print(true_w)

print('trained weight:')
print(net[0].weight.data)

print('true bias:')
print(true_b)

print('trained bias:')
print(net[0].bias.data)
